package com.xiaoyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xiaoyang.common.BaseContext;
import com.xiaoyang.common.CustomException;
import com.xiaoyang.domain.Dish;
import com.xiaoyang.domain.DishFlavor;
import com.xiaoyang.domain.SetmealDish;
import com.xiaoyang.dto.DishDto;
import com.xiaoyang.mapper.DishMapper;
import com.xiaoyang.service.DishFlavorService;
import com.xiaoyang.service.DishService;
import com.xiaoyang.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther XiaoYang
 * @DATE 2022/12/11-19:52
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品，同时保存相对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional//开启事物
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        //获取菜品id
        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);

        BaseContext.close();
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);

        //构造返回的dto对象
        DishDto dishDto = new DishDto();
        //给dto里加数据
        BeanUtils.copyProperties(dish,dishDto);


        //查询菜品对应的口味信息，从dish_flavor查
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

        //给返回的dto加数据
        dishDto.setFlavors(flavors);

        BaseContext.close();

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应的口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //添加当前提交的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

        BaseContext.close();

    }

    /**
     * 根据菜品id删除菜品和该菜品的口味
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithFlavor(Long[] ids) {
        //查看是否停售停售才可删除
        LambdaQueryWrapper<Dish> queryWrapperForCount = new LambdaQueryWrapper<>();
        queryWrapperForCount.in(Dish::getId,ids);
        queryWrapperForCount.eq(Dish::getStatus,1);
        int count = this.count(queryWrapperForCount);
        if (count > 0){
            //抛出异常
            throw new CustomException("菜品未停售,删除失败");
        }

        //通过菜品id(dish_flavor的dish_id)删除表中数据
        for (Long id : ids) {
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(queryWrapper);
        }

        //删除菜品
        List<Long> idList = Arrays.asList(ids);
        this.removeByIds(idList);
        BaseContext.close();

    }

    @Override
    public void updateWithSetmealDish(DishDto dishDto) {
        LambdaUpdateWrapper<SetmealDish> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SetmealDish::getDishId,dishDto.getId());
        updateWrapper.set(SetmealDish::getImage,dishDto.getImage());
        setmealDishService.update(updateWrapper);
        BaseContext.close();
    }
}
